package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.WalletExchangeDao;
import com.jbp.exception.RRException;
import com.jbp.model.Wallet;
import com.jbp.model.WalletExchange;
import com.jbp.model.WalletRecord;
import com.jbp.service.WalletExchangeService;
import com.jbp.service.WalletRecordService;
import com.jbp.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 爱心兑换(WalletExchange)表服务实现类
 * @author cc
 * @since 2020-06-20 13:53:44
 */
@Service
public class WalletExchangeServiceImpl extends ServiceImpl<WalletExchangeDao, WalletExchange> implements WalletExchangeService {
    @Autowired
    private WalletService walletService;
    @Autowired
    private WalletRecordService walletRecordService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WalletExchange> page = this.page(
                new Query<WalletExchange>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public synchronized void exchange(Integer userId, WalletExchange req) {
        //检查余额
        Wallet wallet = walletService.getByUserId(userId);
        if (wallet == null) {
            throw new RRException("用户数据异常");
        }

        req.setNum(BigDecimal.valueOf(req.getNum().intValue()));
        if (req.getNum().intValue()<1) {
            throw new RRException("金额错误");
        }
        if (wallet.getAixin().compareTo(req.getNum()) <0 ) {
            throw new RRException("爱心余额不足");
        }
        //增加兑换记录
        WalletExchange walletExchange = new WalletExchange();
        walletExchange.setUserId(userId);
        walletExchange.setNum(req.getNum());
        save(walletExchange);

        //增加爱心变动记录
        WalletRecord walletRecordAiXin = new WalletRecord();
        walletRecordAiXin.setUserId(userId);
        walletRecordAiXin.setAddType("sub");
        walletRecordAiXin.setCoinType("aixin");
        walletRecordAiXin.setOperationType("exchange");
        walletRecordAiXin.setBeforeBalance(wallet.getAixin());
        walletRecordAiXin.setChangeBalance(req.getNum());
        walletRecordAiXin.setAfterBalance(wallet.getAixin().subtract(req.getNum()));
        walletRecordService.save(walletRecordAiXin);

        //增加心钻变动记录
        WalletRecord walletRecordXinZuan = new WalletRecord();
        walletRecordXinZuan.setUserId(userId);
        walletRecordXinZuan.setAddType("add");
        walletRecordXinZuan.setCoinType("xinzuan");
        walletRecordXinZuan.setOperationType("exchange");
        walletRecordXinZuan.setBeforeBalance(wallet.getXinzuan());
        walletRecordXinZuan.setChangeBalance(req.getNum());
        walletRecordXinZuan.setAfterBalance(wallet.getXinzuan().add(req.getNum()));
        walletRecordService.save(walletRecordXinZuan);

        //修改钱包余额
        wallet.setAixin(wallet.getAixin().subtract(req.getNum()));
        wallet.setXinzuan(wallet.getXinzuan().add(req.getNum()));
        walletService.updateWithLock(wallet);
    }

}